package com.allwees.bs.module.pms.rest;

import com.allwees.bs.module.core.annotation.PrintLog;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.allwees.bs.core.modelbase.vo.R;
import com.allwees.bs.module.pms.dto.ProductDto;
import com.allwees.bs.module.pms.req.AdjustRetailPriceReq;
import com.allwees.bs.module.pms.req.ProductInfoReq;
import com.allwees.bs.module.pms.req.query.OrderProductQueryReq;
import com.allwees.bs.module.pms.req.query.ProductQueryReq;
import com.allwees.bs.module.pms.req.query.ProductsSkusQueryReq;
import com.allwees.bs.module.pms.service.RetailerProductService;
import com.allwees.bs.module.pms.vo.ExportProductSkuBean;
import com.allwees.bs.module.pms.vo.ImportProductReq;
import com.allwees.core.common.util.DateUtil;
import com.allwees.core.common.util.excel.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 零售商产品API
 *
 * @author yangylsky
 * @since 2019-12-12
 */
@PrintLog
@Slf4j
@RestController
@RequestMapping("/backstage/api/pms/product/retailer")
public class RetailerProductApi extends MerchantProductApi<RetailerProductService> {
    /**
     * 供应商调整商品价格（需审核）
     *
     * @param req
     * @return
     */
    @PostMapping("/adjust_retailprice")
    public R adjustRetailPrice(@RequestBody AdjustRetailPriceReq req) {
        boService.adjustRetailPrice(req);
        return R.ok();
    }

    /**
     * 批量导出Producth和Sku
     *
     * @param page
     * @param req
     * @param response
     * @return
     */
    @GetMapping("/export_products_skus")
    public R importProductsSkus(Page<ExportProductSkuBean> page, ProductsSkusQueryReq req, HttpServletResponse response) {
        List<ExportProductSkuBean> beans = boService.exportProductsSkus(page, req);
        String fileName = "ProductsSkus_" + DateUtil.formatDate(new Date()) + ".xlsx";
        ExcelUtil.writeExcel(response, beans, fileName, "ProductsSkus", ExportProductSkuBean.class);
        return null;
    }

    /**
     * 批量导入Producth和Sku
     * @param param
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/import_products_skus", headers = "content-type=multipart/*")
    public R importProductsSkus(ImportProductReq param) throws IOException {
        return doImportProductsSkus(param);
    }


    /**
     * spu商品导出
     *
     * @param response
     * @param req
     * @return
     */
    @GetMapping("/export")
    public R spuExport(HttpServletResponse response, OrderProductQueryReq req) {
        boService.exportHappenOrder(response, req);
        return R.ok();
    }

    /**
     * sku商品导出
     *
     * @param response
     * @return
     */
    @GetMapping("/sku/export")
    public R skuExport(HttpServletResponse response, OrderProductQueryReq req) {
        boService.exportHappenedOrderSkus(response, req);
        return R.ok();
    }

    /**
     * 更新sku编号
     *
     * @param productUuid
     * @param req
     * @return
     */
    @PostMapping("/{productUuid}/update-sku-nos")
    public R updateSkuNos(@PathVariable String productUuid, @RequestBody ProductInfoReq req) {
        boService.updateCustomNos(productUuid, req);
        return R.ok();
    }

    /**
     * 更新sku编号
     *
     * @param productUuid
     * @return
     */
    @GetMapping("/{productUuid}/happened-order-skus")
    public R happenOrderSkus(@PathVariable String productUuid) {
        ProductDto dto = boService.happenOrder(productUuid);
        return R.ok(dto);
    }

    /**
     * 已产生订单的page
     *
     * @param page
     * @param req
     * @return
     */
    @GetMapping("/happened-order-page")
    public R happenOrderPage(Page page, ProductQueryReq req) {
        return R.ok(boService.happenOrderPage(page, req));
    }
}
